//
//  DualMediationMAXManager.m
//  DualMediationSDK
//
//  Created by YeJary on 2025/1/15.
//

#import "DualMediationMAXManager.h"
#import "../../Utils/Logger/IMLogUtils.h"

#import "DMConfigReader.h"
#import "IMDeviceUtils.h"
#import <AppLovinSDK/AppLovinSDK.h>
#import <FirebaseAnalytics/FirebaseAnalytics.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <AdjustSdk/AdjustSdk.h>

#if __has_include(<AppLovinSDK/AppLovinSDK.h>)
#import <AppLovinSDK/AppLovinSDK.h>
#define APPLOVIN_SDK_AVAILABLE 1
#else
#define APPLOVIN_SDK_AVAILABLE 0
#endif
#if __has_include(<FirebaseAnalytics/FirebaseAnalytics.h>)
#import <FirebaseAnalytics/FirebaseAnalytics.h>
#define FIREBASE_AVAILABLE 1
#else
#define FIREBASE_AVAILABLE 0
#endif
#import "DualMediationMAXBannerManager.h"
#import "DualMediationMAXInterstitialManager.h"
#import "DualMediationMAXRewardedVideoManager.h"
#import "DualMediationMAXNativeADManager.h"
#import "DualMediationMAXSplashManager.h"

@interface DualMediationMAXManager()


@end

static DualMediationMAXManager *_maxManager = nil;

@implementation DualMediationMAXManager

+ (instancetype)sharedMAXManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _maxManager = [[self alloc] init];
    });
    return _maxManager;
}

- (void)initMAX
{
    [IMLogUtils info:IMLogTagSDKInit format:@"🚀 [MAX Manager] 开始初始化 MAX SDK"];
    
#if APPLOVIN_SDK_AVAILABLE
#ifdef DEBUG
    // 构建初始化配置
    [ALSdk shared].settings.verboseLoggingEnabled = YES;
    ALSdkInitializationConfiguration *initConfig =
      [ALSdkInitializationConfiguration configurationWithSdkKey:[[DMConfigReader sharedInstance]maxAppKey]
                                                   builderBlock:^(ALSdkInitializationConfigurationBuilder * _Nonnull builder) {
          builder.mediationProvider = ALMediationProviderMAX;
          builder.testDeviceAdvertisingIdentifiers = @[[IMDeviceUtils IDFA]]; // 放这里
        }];
#else
    ALSdkInitializationConfiguration *initConfig =
      [ALSdkInitializationConfiguration configurationWithSdkKey:[[DMConfigReader sharedInstance]maxAppKey]
        builderBlock:^(ALSdkInitializationConfigurationBuilder * _Nonnull builder) {
            builder.mediationProvider = ALMediationProviderMAX;
        }];
#endif
    
    // 初始化 AppLovin SDK
    [[ALSdk shared] initializeWithConfiguration:initConfig
                             completionHandler:^(ALSdkConfiguration *configuration) {
        if (configuration.consentDialogState == ALConsentDialogStateApplies) {
            // Show user consent dialog
        } else if (configuration.consentDialogState == ALConsentDialogStateDoesNotApply) {
            // No consent dialog required
        }
        
        [IMLogUtils info:IMLogTagSDKInit format:@"🚀 [MAX Manager] MAX SDK 初始化完成"];
        
        // Initialize ad managers...
        [[DualMediationMAXBannerManager sharedBannerManager] setBannerDelegate];
        [[DualMediationMAXInterstitialManager sharedInterstitialManager] setInterstitialDelegate];
        [[DualMediationMAXRewardedVideoManager sharedRewardedVideoManager] setRewardedVideoDelegate];
//        [[DualMediationMAXNativeADManager sharedNativeADManager] initWithNativead];
        [[DualMediationMAXSplashManager sharedSplashManager] initSplashDelegate];
    }];
#else
    [IMLogUtils warning:IMLogTagSDKInit format:@"🚀 [MAX Manager] AppLovin SDK 不可用，跳过初始化"];
#endif
}

@end
